package cn.uway.config;


import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.RedisFlushMode;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
//import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
import org.springframework.util.StringUtils;



/**
 * 继承并重写RedisHttpSessionConfiguration.java，实现在properties中配置session有效期
 * 如果在注解中写死session有效期，则无需重写，直接增加@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 120) 即可
 */
@Configuration
/** @EnableRedisHttpSession /*(maxInactiveIntervalInSeconds = 120)*/  //设置session超时时间,单位秒，支持redis
/**** @EnableRedisHttpSession这个注解，加了它之后，spring 会生产一个新的拦截器，用来实现Session共享的操作   ****/
public class RedisSessionConfiguration extends RedisHttpSessionConfiguration {

	 
	@Value("${spring.session.maxInactiveIntervalInSeconds}")
	private  Integer  maxInactiveIntervalInSeconds;
	
	private RedisSerializer<Object> defaultRedisSerializer;
    
	private String redisNamespace = "";

	private RedisFlushMode redisFlushMode = RedisFlushMode.ON_SAVE;
	
	@Override
	public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) {
		this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
		super.setMaxInactiveIntervalInSeconds(maxInactiveIntervalInSeconds);
	}

	
	@Override
	@Bean
	public RedisTemplate<Object, Object> sessionRedisTemplate(
			RedisConnectionFactory connectionFactory) {
		RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
		template.setKeySerializer(new StringRedisSerializer());
		template.setHashKeySerializer(new StringRedisSerializer());
		if (this.defaultRedisSerializer != null) {
			template.setDefaultSerializer(this.defaultRedisSerializer);
		}
		template.setConnectionFactory(connectionFactory);
		return template;
	}
	
	@Override
	@Bean
	public RedisOperationsSessionRepository sessionRepository(
			@Qualifier("sessionRedisTemplate") RedisOperations<Object, Object> sessionRedisTemplate,
			ApplicationEventPublisher applicationEventPublisher) {
		RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository(
				sessionRedisTemplate);
		sessionRepository.setApplicationEventPublisher(applicationEventPublisher);
		sessionRepository
				.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds);
		if (this.defaultRedisSerializer != null) {
			sessionRepository.setDefaultSerializer(this.defaultRedisSerializer);
		}

		String redisNamespace = getRedisNamespace();
		if (StringUtils.hasText(redisNamespace)) {
			sessionRepository.setRedisKeyNamespace(redisNamespace);
		}

		sessionRepository.setRedisFlushMode(this.redisFlushMode);
		return sessionRepository;
	}
	
	
	private String getRedisNamespace() {
		if (StringUtils.hasText(this.redisNamespace)) {
			return this.redisNamespace;
		}
		return System.getProperty("spring.session.redis.namespace", "");
	}

}
